package eu.runelord.blog.singleton;

public class Main {

	public static void main(String[] args) {
		ISingleton singleton = Singleton.getSingleton(null);
		System.out.println(singleton.helloWorld());
	}

}

class Singleton implements ISingleton {

	private static Singleton singleton;
	
	private String message; 

	public static Singleton getSingleton(String message) {
		if (singleton == null)
			synchronized (SingletonFactory.class) {
				if (singleton == null)
					singleton = new Singleton();
			}
		singleton.message = message;
		return singleton;
	}

	public static Singleton getSingleton() {
		if (singleton == null)
			synchronized (SingletonFactory.class) {
				if (singleton == null)
					singleton = new Singleton();
			}
		return singleton;
	}
	
	private Singleton() {
	}

	public String helloWorld() {
		return message;
	}

}

class SingletonFactory {

	private static ISingleton singleton;

	public static ISingleton getSingleton() {
		if (singleton == null)
			synchronized (SingletonFactory.class) {
				if (singleton == null)
					singleton = new ISingleton() {

						public String helloWorld() {
							return "Hello World";
						}
					};
			}
		return singleton;
	}
}

interface ISingleton {
	public String helloWorld();
}